package com.powernobug.servlet;

import com.powernobug.bean.vo.BaseRespVo;
import com.powernobug.bean.vo.param.GoodsCreateParam;
import com.powernobug.service.AdminGoodsService;
import com.powernobug.service.impl.AdminGoodsServiceImpl;
import com.powernobug.util.JsonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: 肖云戈
 * @CreateTime: 2024-08-22
 * @Description:
 */
@WebServlet("/goods/*")
public class AdminGoodsServlet extends CommonServlet {
    private static AdminGoodsService adminGoodsService = new AdminGoodsServiceImpl();

    public BaseRespVo create(HttpServletRequest req) throws IOException {
        // 从Json字符串中，解析为GoodsCreateParam对象
        String jsonQuery = req.getReader().readLine();
        GoodsCreateParam goodsCreateParam = JsonUtil.readJson(jsonQuery, GoodsCreateParam.class);
        // 对必选项进行校验，如果必选项字段为null，则直接返回errList
        if (goodsCreateParam.getGoods().getGoodsSn() == null || goodsCreateParam.getGoods().getName() == null)
            return BaseRespVo.errList(701);
        // 将解析到的GoodsCreateParam对象传入model层进行数据插入，返回值为boolean类型，标识插入是否成功
        boolean isSuccess = adminGoodsService.creatGood(goodsCreateParam);
        return isSuccess ? BaseRespVo.okList(null) : BaseRespVo.errList(703);
    }
}
